//
//  UIListViewCell.m
//  Vgo
//
//  Created by 21CN Vgo on 11-4-6.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "UIListViewCell.h"

@interface UIListViewCell (private)
-(void)initialize;

@end
 
@implementation UIListViewCell

@synthesize imageView = _imageView;
@synthesize textLabel = _textLabel;
@synthesize reuseIdentifier = _reuseIdentifier;
@synthesize selectionStyle = _selectionStyle;
@synthesize selected = _selected;
@synthesize editingStyle = _editingStyle;
@synthesize editing = _editing;
@synthesize showingDeleteConfirmation = _showingDeleteConfirmation;
@synthesize cellStyle = _cellStyle;

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
//- (void)drawRect:(CGRect)rect {
//    // Drawing code.
//	[super drawRect:rect];
//}


- (void)dealloc {
	SAFE_RELEASE(_reuseIdentifier);
	SAFE_RELEASE(_imageView);
	SAFE_RELEASE(_textLabel);
    [super dealloc];
}

#define DEFAULT_REUSEIDENDTIFIER @"ListViewCell"
#define DEFAULT_CELLWIDTH 100.0
#define DEFAULT_CELLHEIGHT 150.0

// Designated initializer.  If the cell can be reused, you must pass in a reuse identifier.  You should use the same reuse identifier for all cells of the same form.  
- (id)initWithStyle:(UIListViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
	// Initialization code.
	return [self initWithFrame:CGRectMake(0, 0, DEFAULT_CELLWIDTH, DEFAULT_CELLHEIGHT) style:style reuseIdentifier:reuseIdentifier];
}	

- (id)_selfInitWithStyle:(UIListViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
	_cellStyle = style;
	if (reuseIdentifier != nil && [reuseIdentifier length] > 0) {
		_reuseIdentifier = [[NSString alloc]initWithString:reuseIdentifier];
	} else {
		_reuseIdentifier = [[NSString alloc]initWithString:DEFAULT_REUSEIDENDTIFIER];
	}
	self.autoresizingMask = UIViewAutoresizingNone;
	
	[self initialize];

	return self;
}

+ (id)listViewCellWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle reuseIdentifier:(NSString *)reuseIdentifier
{
	UIListViewCell * cell = nil;
	
	if (nibName == nil || [nibName length] == 0 || nibBundle == nil) {
		return cell;
	}
	NSArray * nib = [nibBundle loadNibNamed:nibName owner:nil options:nil];
	cell = [nib objectAtIndex:0];
	 
	if (cell) {
		[cell _selfInitWithStyle:UIListViewCellStyleCustom reuseIdentifier:reuseIdentifier];
	}
	
	return cell;
}

- (id)initWithFrame:(CGRect)theFrame style:(UIListViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
	self = [super initWithFrame:theFrame];
    if (self) { 
        // Initialization code.
		[self _selfInitWithStyle:style reuseIdentifier:reuseIdentifier];
    }
    return self;
}

// if the cell is reusable (has a reuse identifier), this is called just before the cell is returned from the list view method dequeueReusableCellWithIdentifier:.  If you override, you MUST call super.
- (void)prepareForReuse
{
}
 
// animate between regular and selected state
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
	_selected = selected;
	if (_selected) {
		switch (self.selectionStyle) {
			case UIListViewCellSelectionStyleNone:
				break;
			case UIListViewCellSelectionStyleBlue:
				[self setBackgroundColor:[UIColor blueColor]];
				break;
			case UIListViewCellSelectionStyleGray:
				[self setBackgroundColor:[UIColor grayColor]];
				break;
			default:
				break;
		}
	} else {
		[self setBackgroundColor:[UIColor clearColor]];
	}

}

// animate between regular and highlighted state                
//- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated;              

// show appropriate edit controls (+/- & reorder). By default -setEditing: calls setEditing:animated: with NO for animated.                 
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
	_editing = editing;
}

#pragma mark private
#define DEFAULT_IMAGEHEIGHT 120.0
-(void)initialize
{
	_selectionStyle = UIListViewCellSelectionStyleNone;
	
	if (_cellStyle == UIListViewCellStyleDefault) {
		self.frame = CGRectMake(0, 0, DEFAULT_CELLWIDTH, DEFAULT_IMAGEHEIGHT);
		_imageView = [[UIImageView alloc] initWithFrame:self.bounds];
		_imageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin |
		UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight;
		[_imageView setBackgroundColor:[UIColor clearColor]];
		[self addSubview:_imageView];
		
		_textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, DEFAULT_IMAGEHEIGHT + 1, 
															   DEFAULT_CELLWIDTH, DEFAULT_CELLHEIGHT - DEFAULT_IMAGEHEIGHT - 1)];
		_textLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin |
		UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight;	
		_textLabel.textAlignment = UITextAlignmentCenter;
		[_textLabel setBackgroundColor:[UIColor clearColor]];
		[self addSubview:_textLabel];
	}
}

#pragma mark NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder
{
	[super encodeWithCoder:aCoder];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
	return [super initWithCoder:aDecoder];
}

//-(void)ListviewEnterSingleEditMode
//{
//}
//
//-(void)ListviewCancelSingleEditMode
//{
//}

@end
